#include<bits/stdc++.h>
using namespace std;
const int maxn = 1001;
char a[maxn][maxn];
int vis[maxn][maxn];
int d[4][2] = {{-1 , 0} , {0 , -1} , {1 , 0} , {0 , 1}};
struct node{
	int x;
	int y;
}q[maxn];
int n , m;
void bfs(int x , int y){
	int head = 1;
	int tail = 1;
	q[tail].x = x;
	q[head].y = y;
	tail++;
	while(head < tail){
		int x0 = q[head].x;
		int y0 = q[head].y;
		vis[x0][y0] = 1;
		for(int i = 0 ; i < 4 ; i++){
			int nx = x0 + d[i][0];
			int ny = y0 + d[i][1];
			if(nx >= 0 && nx < n && ny >= 0 && ny < m && a[nx][ny]!= '0' && vis[nx][ny] != 1){
				q[tail].x = nx;
				q[tail].y = ny;
				tail++;
			}
		}
		head++;
	}
}
int main(){
	int cnt = 0;
	cin >> n >> m;
	memset(vis , 0 , sizeof(vis));
	for(int i = 0 ; i < n ; i++) scanf("%s" , a[i]);
	for(int i = 0 ; i < n ; i++){
		for(int j = 0 ; j < m ; j++){
			if(a[i][j] != '0' && !vis[i][j]){
				cnt++;
				bfs(i , j);
			}
		}
	}
	cout << cnt << endl;
	return 0;
}
